package com.itbbs.redis.controller;/**
 * @Describe：
 * @Author： liuyingao
 * @date： 2021-05-30-16:27
 */

import com.itbbs.encrypt.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import javax.annotation.Resource;

/**
 * @author liuyingao
 * @Describe:[]
 * @title RedisController
 * @create 2021-05-30 16:27
 * @desc RedisController
 **/
@Api(tags = "redis接口测试")
@RestController
@RequestMapping("/redis")
public class RedisController {

    // 注入实现读写分离
    @Resource(name = "getReadJedisCluster")
    private JedisCluster readJedisCluster;

    @Resource(name = "getWriteJedisCluster")
    private JedisCluster writeJedisCluster;


    /**
     * @Describe: [增加一个数据项]
     * @param: [key, value]
     * @return: void
     * @title: set
     * @author: liuyingao
     * @time: 2021/5/30
     */
    @ApiOperation(value = "增加一个数据项，没有设置过期时间")
    @PostMapping("/set/{key}/{value}")
    public void set(
            @PathVariable("key") String key,
            @PathVariable("value") String value) {
        try {
            writeJedisCluster.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * @Describe: [增加一个带过期时间的数据项]
     * @param: [key, value, second]
     * @return: void
     * @title: setSecond
     * @author: liuyingao
     * @time: 2021/5/30
     */
    @ApiOperation(value = "增加一个带过期时间的数据项")
    @PostMapping("/setSecond/{key}/{value}/{second}")
    public void setSecond(
            @PathVariable("key") String key,
            @PathVariable("value") String value,
            @PathVariable("second") int second) {
        try {
            key = MD5.encrypt(key);
            System.out.println(key);
            writeJedisCluster.setex(key, second, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @Describe: [删除一个数据项]
     * @param: [key]
     * @return: void
     * @title: delete
     * @author: liuyingao
     * @time: 2021/5/30
     */
    @ApiOperation(value = "删除一个数据项")
    @PostMapping("/delete/{key}")
    public void delete(@PathVariable("key") String key) {
        try {
            writeJedisCluster.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Describe: [读取一个数据项]
     * @param: [key]
     * @return: java.lang.String
     * @title: get
     * @author: liuyingao
     * @time: 2021/5/30
     */
    @ApiOperation(value = "读取一个数据项")
    @PostMapping("/get/{key}")
    public String get(@PathVariable("key") String key) {
        try {
            String value = readJedisCluster.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
